package com.atguigu.Threads.Exer;

/**练习：创建两个子线程，分别实现输出1到100的偶数和奇数
 * @author xiaoqi
 * @data 2021/04/02 0:04
 */
public class ThreadDemo {
    public static void main(String[] args) {
        Mythread1 t1 = new Mythread1();
        Mythread2 t2 = new Mythread2();
        t1.start();
        new Thread(){
            @Override
            public void run() {
                for (int i = 1; i < 100; i++) {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(i % 2 == 1){
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
    }
}

class Mythread1 extends Thread{
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            if(i % 2 == 0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}
class Mythread2 extends Thread{
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i % 2 == 1){
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}
